package com.eco.utils;

import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

@Slf4j
public class DataMapUtil {
    private static Map<String, Object> map = new ConcurrentHashMap<String, Object>();

    public static void add(String name, Object obj) {
        log.info("add fxml {}", name);
        map.put(name, obj);
    }

    /**
     * 根据key获取value
     */
    public static Object get(String name) {
        log.info("get fxml {}", name);
        return map.get(name);
    }

    /**
     * 获取所有key
     */
    public static List<String> getKeys() {
        return map.keySet().stream().collect(Collectors.toList());
    }

    /**
     * 根据value移除key-value
     */
    public static String remove(Object obj) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == obj) {
                map.remove(entry.getKey());
                return entry.getKey().toString();
            }
        }
        return null;
    }

    /**
     * 根据value获取key
     */
    public static String get(Object obj) {
        for (Map.Entry entry : map.entrySet()) {
            if (entry.getValue() == obj) {
                return entry.getKey().toString();
            }
        }
        return null;
    }

}
